/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.university.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.university.entity.DisciplineFields;
import com.thinkgem.jeesite.modules.university.entity.XueLiLevel;
import com.thinkgem.jeesite.modules.university.entity.MajorCategory;
import com.thinkgem.jeesite.modules.university.service.MajorCategoryService;

/**
 * 专业类别Controller
 * @author 朱俊吉
 * @version 2017-12-08
 */
@Controller
@RequestMapping(value = "${adminPath}/university/majorCategory")
public class MajorCategoryController extends BaseController {

	@Autowired
	private MajorCategoryService majorCategoryService;
	
	@ModelAttribute
	public MajorCategory get(@RequestParam(required=false) String id) {
		MajorCategory entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = majorCategoryService.get(id);
		}
		if (entity == null){
			entity = new MajorCategory();
		}
		return entity;
	}
	
	@RequiresPermissions("university:majorCategory:view")
	@RequestMapping(value = {"list", ""})
	public String list(MajorCategory majorCategory, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<MajorCategory> page = majorCategoryService.findPage(new Page<MajorCategory>(request, response), majorCategory); 
		model.addAttribute("page", page);
		return "modules/university/majorCategoryList";
	}

	@RequiresPermissions("university:majorCategory:view")
	@RequestMapping(value = "form")
	public String form(MajorCategory majorCategory, Model model) {
		model.addAttribute("majorCategory", majorCategory);
		return "modules/university/majorCategoryForm";
	}

	@RequiresPermissions("university:majorCategory:edit")
	@RequestMapping(value = "save")
	public String save(MajorCategory majorCategory, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, majorCategory)){
			return form(majorCategory, model);
		}
		majorCategoryService.save(majorCategory);
		addMessage(redirectAttributes, "保存专业类别成功");
		return "redirect:"+Global.getAdminPath()+"/university/majorCategory/?repage";
	}
	
	@RequiresPermissions("university:majorCategory:edit")
	@RequestMapping(value = "delete")
	public String delete(MajorCategory majorCategory, RedirectAttributes redirectAttributes) {
		majorCategoryService.delete(majorCategory);
		addMessage(redirectAttributes, "删除专业类别成功");
		return "redirect:"+Global.getAdminPath()+"/university/majorCategory/?repage";
	}
	
	@ResponseBody
	@RequestMapping(value = "listDataBySelecteddisciplineFields")
	public List<MajorCategory> listDataBySelecteddisciplineFields(@RequestParam(required=false) String selected) {
		MajorCategory majorCategory = new MajorCategory();
		majorCategory.setDisciplineFields(new DisciplineFields(selected));
		return majorCategoryService.findList(majorCategory);
	}

}